import java.util.Scanner;

public class BloodReservoirTest extends BloodReservoir {
    int soldUnits = 0;  // so mau ban
    
    public BloodReservoirTest() {
        super();
    }
    
    public BloodReservoirTest(int u, double p) {
        super(u, p);
    }
    
    public void input(){
       
        Scanner in = new Scanner(System.in);
        int volume, eVolume ;
        double p;
        String str;
        try { 
            do {  
                System.out.print("\n Input Normal Volume : ");
                str = in.next();
                volume = Integer.valueOf(str);
            } while(volume <= 0);
            
            do{
                System.out.print("Input volume for emergency purpose :");
                str = in.next();
                eVolume = Integer.valueOf(str);
            } while(eVolume <= 0);
            
            do{
                System.out.print("Input price of a pint of blood: ");
                p = in.nextDouble();
            }while(p <= 0);
            
            setUnits(volume + eVolume);
            setCostOfUnit(p);
            soldUnits = 0;
        } catch(Exception e) {
            System.out.print("Must input a Numberic > 0");   
            input();
        }
    }
    
    public void selling(int amount) {
        if(amount > this.units) {
            System.out.println("Units in counter is insufficient");
        } else {
            System.out.println("Sold " + amount + "units of blood ");
            units -= amount;
            soldUnits = amount;
        }
    }
    
    public void output(){
        System.out.print("Units in stock: " + units);
        System.out.print(", Price: " + costOfUnit);
        System.out.print(", Sold: " + soldUnits);
        System.out.println(" Total: " + (costOfUnit * soldUnits));
    }
    
    public static void main(String args[]) {
        BloodReservoirTest counter1 = new BloodReservoirTest();
        BloodReservoirTest counter2 = new BloodReservoirTest();
        BloodReservoirTest counter3 = new BloodReservoirTest();
        
        System.out.println("\n ---Input Infomation about counter 1--- ");
        counter1.input();
        
        System.out.println("\n ---Input Infomation about counter 2--- ");
        counter2.input();
        
        System.out.println("\n ---Input Infomation about counter 3--- ");
        counter3.input();
        
        Scanner in = new Scanner(System.in);
        int counterIndex;
        int amount;
        String cont;
        do {
            try {
                do {
                    System.out.print("\nWhat counter you want books blood? 1/2/3: ");
                    counterIndex = in.nextInt();
                    if (counterIndex < 1 || counterIndex > 3) {
                        System.out.println("Counter " + counterIndex + " not avaiable.Re_input please: ");
                    }
                } while(counterIndex < 1 || counterIndex > 3);
                
                do {
                    System.out.print("How many amount of blood pints you want to book: ");
                    amount = in.nextInt();
                    if(amount < 30) {
                        System.out.println("Sales only MORE THAN 30 pints!");
                    }
                } while(amount <= 30);
                
                BloodReservoirTest currentCounter;
                currentCounter = counterIndex == 1 ? counter1 : counterIndex == 2 ? counter2:counter3;
                currentCounter.selling(amount);
            }catch(Exception e){
                System.out.println("Must input a numberic!");
            }
            System.out.print("Continue Y/N: ");
            cont = in.next().toUpperCase();
            if(cont.startsWith("N")) {
                System.out.println("\nStatis infomation \n");
                System.out.print("Counter1: \n\t");
                counter1.output();
                System.out.print("Counter2: \n\t");
                counter2.output();
                System.out.print("Counter3: \n\t");
                counter3.output();
                System.out.println("\nTotal Units in the bank: " + (counter1.getUnits() + counter2.getUnits() + counter3.getUnits()));
            }
        }while(cont.startsWith("Y"));
    }
}
